<?php

namespace app\admin\controller;

use app\common\business\AdminRole;
use app\common\lib\Arr;
use app\common\lib\Param;
use app\common\lib\Show;
use think\response\Json;

class Links extends AdminBase
{
    /**
     * 管理后台列表
     * @return Json
     */
    public function list(): Json
    {
        list($keyword, $page,$limit) = Param::getMore([
            ['name', ''],
            ['page', null],
            ['limit', null],
        ], $this->request, true);
        try {
            $result=(new \app\common\business\Links())->getTableList($page,$limit,$keyword);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        $result['list'] = $this->resetInfo($result['list']);
        return Show::success($result);
    }

    /**
     * 新增/保存数据
     * @return Json
     */
    public function save(): Json
    {
        list($id, $status, $name, $sort, $category_id, $url) = Param::postMore([
            ['id',null],
            ['status', 1],
            ['name',''],
            ['sort', 0],
            ['category_id', 0],
            ['url', ''],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
            'name'=>$name,
            'sort'=>$sort,
            'category_id'=>$category_id,
            'url'=>$url,
        ];
        $validate = new \app\admin\validate\Links();
        $scene=empty($data['id'])?'create':'update';
        if (!$validate->scene($scene)->check($data)) {
            return Show::error([], $validate->getError());
        }
        try {
            $result = (new \app\common\business\Links())->save($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 修改状态
     * @return Json
     */
    public function updateStatus(): Json
    {
        list($id, $status) = Param::postMore([
            ['id',null],
            ['status', null],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
        ];
        try {
            $result = (new \app\common\business\Links())->updateStatus($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 根据id删除
     * @return Json
     */
    public function deleteById(): Json
    {
        $id=$this->request->get('id');
        try {
            $result=(new \app\common\business\Links())->deleteById($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "删除成功");
        }
        return Show::error([], '删除失败');
    }

    public function resetInfo($list){
        if (empty($list)){
            return [];
        }

        //字典
        $categoryIdArr = array_column($list,'category_id');
        $mapCategory = [
            ['id','in',$categoryIdArr]
        ];
        $categoryData = (new \app\common\business\Category())->getListByWhere($mapCategory,0,0,'id,name');
        $categoryArr = Arr::getKeyValueByTwoArray($categoryData,'','id',2);

        foreach ($list as $k=>$v){
            $list[$k]['category_text'] = $categoryArr[$v['category_id']] ? $categoryArr[$v['category_id']]['name'] : '';
        }
        return $list;
    }
}
